home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / appl / fingerd / fingerd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  5.0 KB  |  238 lines

  1. RCS_ID_C= "$Id: fingerd.c,v 3.2 1994/05/02 19:29:43 jraja Exp $";
  2. /*
  3.  * fingerd.c --- an example of TCP daemon for AmiTCP/IP
  4.  *
  5.  * This command sends either a AmiTCP/IP banner or the specified file
  6.  * into the TCP socket server_socket 
  7.  *
  8.  * Author: ppessi <Pekka.Pessi@hut.fi>
  9.  *
  10.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  11.  *                  Helsinki University of Technology, Finland.
  12.  *
  13.  * Created      : Mon May 24 23:44:43 1993 ppessi
  14.  * Last modified: Mon Oct 18 17:34:41 1993 ppessi
  15.  *
  16.  * $Log: fingerd.c,v $
  17.  * Revision 3.2  1994/05/02  19:29:43  jraja
  18.  * Updated for the new net.lib.
  19.  *
  20.  * Revision 3.1  1994/02/26  22:39:49  ppessi
  21.  * Updated major version number for release 3
  22.  *
  23.  * Revision 1.4  1993/10/18  15:45:30  ppessi
  24.  * Added real version tags.
  25.  *
  26.  * Revision 1.3  1993/10/15  01:24:00  ppessi
  27.  * A new version supporting real fingering. Uses Apipe:.
  28.  *
  29.  * Revision 1.2  1993/08/10  20:46:23  jraja
  30.  * Added version string.
  31.  *
  32.  * Revision 1.1  1993/06/04  11:49:53  jraja
  33.  * Initial revision
  34.  *
  35.  */
  36.  
  37. #include "fingerd_rev.h"
  38. const char version[] = VERSTAG;
  39.  
  40. char copyright[] =
  41.   "Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>\n"
  42.   "Helsinki University of Technology, Finland.\n";
  43.  
  44. #ifdef AMIGA
  45. #if __SASC
  46. #include <proto/socket.h>
  47. #include <proto/dos.h>
  48. #include <clib/exec_protos.h>
  49. #include <pragmas/exec_sysbase_pragmas.h>
  50. #elif __GNUC__
  51. #include <inline/socket.h>
  52. #include <inline/exec.h>
  53. #else
  54. #include <clib/socket_protos.h>
  55. #endif
  56. #endif /* AMIGA */
  57.  
  58. #include <errno.h>
  59. #include <netdb.h>
  60.  
  61. #include <sys/param.h>
  62. #include <sys/socket.h>
  63. #include <sys/ioctl.h>
  64. #include <netinet/in.h>
  65.  
  66. #include <signal.h>
  67.  
  68. #include <dos/dos.h>
  69. #include <exec/execbase.h>
  70. #include <dos/var.h>
  71.  
  72. #include <stdlib.h>
  73. #include <clib/netlib_protos.h>
  74.  
  75. #include "pathnames.h"
  76.  
  77. #define isspace(x) (x == ' ')
  78.  
  79. extern struct ExecBase *SysBase;
  80.  
  81. BPTR Stdin = NULL;
  82. BPTR Stdout = NULL;
  83. BPTR Stderr = NULL;
  84.  
  85. void
  86. _STIdosStdio(void)
  87. {
  88.   struct Process *p = (struct Process *)SysBase->ThisTask;
  89.  
  90.   Stdin = p->pr_CIS;
  91.   Stdout = p->pr_COS;
  92.   Stderr = p->pr_CES ? p->pr_CES : Stdout;
  93. }
  94.  
  95. #define BANNER \
  96. "\r\n             AmiTCP/IP 3.0 Release System.\r\n\r\n"
  97.  
  98. int
  99. main(int argc, char **argv)
  100. {
  101.   long len;
  102.   int s = init_inet_daemon();
  103.   BPTR pfh;
  104.  
  105. # define CMDHEAD "apipe:" _PATH_FINGER ""
  106. # define CMDLEN (sizeof(CMDHEAD)-1)
  107. # define LINELEN (1024)
  108. # define CMDLINELEN (1024+CMDLEN)
  109.  
  110.   char line[LINELEN], cmdline[CMDLINELEN] = CMDHEAD;
  111.   char *fname = cmdline;
  112.  
  113. #ifdef LOGGING
  114. #include <netinet/in.h>
  115.   struct sockaddr_in sin;
  116.   int sval;
  117. #endif
  118.  
  119.   if (s == -1) {
  120. #ifdef STANDALONE
  121.     struct sockaddr_in sin;
  122.  
  123.     s = serveraccept("finger", &sin);
  124.     if (s != -1) {
  125.       FPrintf(Stderr, "Accepted a connection from %s, port %ld\n",
  126.           inet_ntoa(sin.sin_addr), sin.sin_port);
  127.     } else 
  128. #endif
  129.       return 1;
  130.   }
  131.  
  132. #ifdef LOGGING            /* unused for now */
  133.   sval = sizeof(sin);
  134.   if (getpeername(0, &sin, &sval) < 0)
  135.     fatal("getpeername");
  136. #endif
  137.  
  138.   {
  139.     enum { WSPACE, TOKEN, QUOTED } state = WSPACE;
  140.     char c;
  141.     int received, peek, len = 0;
  142.  
  143.     do {
  144.       received = recv(s, line + len, 1, 0);
  145.       if (received < 0) {
  146.     PrintNetFault(Errno(), "recv");
  147.     return 1;
  148.       }
  149.     } while (received && line[len] != '\n' && len++ < LINELEN - 2);
  150.  
  151.     if (len == 1 && argc > 1) 
  152.       fname = argv[1];
  153.  
  154.     if (line[len - 1] == '\r') 
  155.       len--;
  156.     line[len] = '\0';
  157.  
  158.     /* 
  159.      * Parse command line:
  160.      * Quote arguments,
  161.      * convert /w to -l
  162.      */
  163.     peek = 0; received = len = CMDLEN;
  164.     while ((c = line[peek++]) && len < CMDLINELEN - 4) {
  165.       switch(state) {
  166.       case WSPACE:
  167.     if (c == '/' 
  168.         && (line[peek] == 'W' || line[peek] == 'w') 
  169.         && (line[peek + 1] == '\0' || isspace(line[peek + 1]))) { 
  170.       cmdline[len++] = ' ';
  171.       cmdline[len++] = '-';
  172.       cmdline[len++] = 'l';
  173.       peek += 1;
  174.       received = len;
  175.     } else if (c == '"') {
  176.       cmdline[len++] = '"';
  177.       state = QUOTED;
  178.     } else if (!isspace(c)) {
  179.       cmdline[len++] = ' ';
  180.       cmdline[len++] = '"';
  181.       cmdline[len++] = c;
  182.       state = TOKEN;
  183.     } 
  184.     break;
  185.       case TOKEN:
  186.     if (c == '"') {
  187.       state = QUOTED;
  188.     } else if (isspace(c)) {
  189.       cmdline[len++] = '"';
  190.       state = WSPACE;
  191.       received = len;
  192.     } else {
  193.       cmdline[len++] = c;
  194.     }
  195.     break;
  196.       case QUOTED:
  197.     if (c == '"') {
  198.       state = TOKEN;
  199.     } else {
  200.       cmdline[len++] = c;
  201.     }
  202.     break;
  203.       }
  204.     }
  205.     if (c || state == QUOTED) {
  206.       /* remove last argument */
  207.       len = received;
  208.     } else if (state == TOKEN) {
  209.       cmdline[len++] = '"';
  210.     }
  211.     cmdline[len] = '\0';    /* put eos */
  212.   }
  213.  
  214.   pfh = Open(fname, MODE_OLDFILE);
  215.   
  216.   /* Try to open banner if there is no APipe or Finger */
  217.   if (pfh || 
  218.       argc >= 2 && (pfh = Open(argv[1], MODE_OLDFILE))) {
  219.     while (FGets(pfh, line, sizeof(line))) {
  220.       len = strlen(line);
  221.       /* Change eol's to the network standard */
  222.       if (line[len - 1] == '\n') {
  223.     line[len - 1] = '\r';
  224.     line[len++] = '\n';
  225.       }
  226.       if (send(s, line, len, 0) < 0) break;
  227.     }
  228.     Close(pfh);
  229.   } else {
  230.     if (send(s, BANNER, strlen(BANNER), 0) < 0) {
  231.       PrintNetFault(Errno(), "send");
  232.       return 1;
  233.     }
  234.   }
  235.   
  236.   return 0;
  237. }
  238.